package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.service.WmNewsMaterialService;
import com.heima.wemedia.service.WmNewsService;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import okhttp3.*;
import org.json.JSONObject;

import java.io.*;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private WmNewsMapper wmNewsMapper;

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
    String API_KEY = "buc56tPK9O25vd1g2ECx2F5O";
    String SECRET_KEY = "3ooMPquyDMzDY7DZhftdJ2w17uPyzyg9";

    /**
     * 查询文章列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmNewsPageReqDto dto) {
        //1.非空判断
        dto.checkParam();
        //2.分页查询
        IPage<WmNews> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmNews> queryWrapper = Wrappers.lambdaQuery();
        //2.1 根据状态查询
        if (dto.getStatus() != null) {
            queryWrapper.eq(WmNews::getStatus, dto.getStatus());
        }

        //2.2 根据关键字查询
        if (!StringUtils.isEmpty(dto.getKeyword())) {
            queryWrapper.like(WmNews::getTitle, dto.getKeyword());
        }

        //2.3 根据频道id查询
        if (dto.getChannelId() != null) {
            queryWrapper.eq(WmNews::getChannelId, dto.getChannelId());
        }

        //2.4 根据开始和结束时间查询
        if (dto.getBeginPubdate() != null && dto.getEndPubdate() != null) {
            queryWrapper.between(WmNews::getPublishTime, dto.getBeginPubdate(), dto.getEndPubdate());
        }

        //2.5 根据登录用户id查询
        queryWrapper.eq(WmNews::getUserId, WmThreadLocalUtil.getUser().getId());
        queryWrapper.orderByDesc(WmNews::getCreatedTime);

        page = super.page(page, queryWrapper);

        //3.返回数据结果
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Transactional
    @Override
    public ResponseResult submit(WmNewsDto dto) throws JSONException {
        Short isSubmit = dto.getStatus();

        //1.判断必要参数不能为空
        if (StringUtils.isBlank(dto.getTitle()) || StringUtils.isBlank(dto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2.创建或修改文章
        //2.1 复制dto给pojo
        WmNews wmNews = new WmNews();
        BeanUtils.copyProperties(dto, wmNews);
        //2.2 单独处理images的值（将dto里的封面图片列表转为逗号拼接的地址字符串）
        if (dto.getImages() != null && dto.getImages().size() > 0) {
            //String coverImageStr = String.join(",", dto.getImages());
            // String coverImageStr =  StringUtils.join(dto.getImages(), ",");
            String coverImageStr = dto.getImages().stream().collect(Collectors.joining(","));
            wmNews.setImages(coverImageStr);
        }
        //2.3 当布局方式为自动布局时，则需要将布局方式设置为临时为空（后续逻辑还会继续决定布局方式和更新布局方式）
        if (dto.getType() == -1) {
            wmNews.setType(null);
        }
        //2.4 根据id参数值决定创建文章还是修改文章
        ResponseResult responseResult = this.saveOrUpdateWmNews(dto, isSubmit, wmNews);
        if (responseResult != null) {
            return responseResult;
        }

        //必须是提交审核状态，才能执行后续更多逻辑
        if (isSubmit.equals(WmNews.Status.SUBMIT.getCode())) {
            //3.从文章内容中抽取全部图片地址列表
            List<String> contentImageUrlList = extractContentImageUrlList(dto);

            //4.保存内容图片与文章关系
            if (contentImageUrlList.size() > 0) {
                responseResult = this.saveRelation(contentImageUrlList, wmNews.getId(), (short) 0);
                if (responseResult != null) {
                    return responseResult;
                }
            }

            //5.保存封面图片与文章关系
            responseResult = this.saveRelationForCoverImage(dto, wmNews, contentImageUrlList);
            if (responseResult != null) {
                return responseResult;
            }
            //6.审核文章
            //重新查询dto
            dto.setId(wmNews.getId());
            autoAuditContent(dto);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 保存封面图片与文章关系
     *
     * @param dto
     * @param wmNews
     * @param contentImageUrlList
     * @return
     */
    private ResponseResult saveRelationForCoverImage(WmNewsDto dto, WmNews wmNews, List<String> contentImageUrlList) {
        ResponseResult responseResult;
        //1 获取dto封面图片地址列表
        List<String> coverImageUlrList = dto.getImages();

        //2 如果是自动布局，则根据自动匹配规则决定最终封面布局方式及更新文章布局方式和封面图片
        if (dto.getType() == -1) {
            int size = contentImageUrlList.size();

            //2.1 根据自动匹配规则决定封面图片和布局方式
            //自动匹配规则1：如果内容图片地址数量大于等于3，则取前3张图片当做封面图片，布局决定为多图布局
            if (size >= 3) {
                //coverImageUlrList = contentImageUrlList.subList(0,3);
                coverImageUlrList = contentImageUrlList.stream().limit(3).collect(Collectors.toList());
                wmNews.setType((short) 3);//多图布局
            } else if (size >= 1 && size < 3) {
                //自动匹配规则2：如果内容图片地址数量大于等于1，则取前1张图片当做封面图片，布局决定为单图布局
                coverImageUlrList = contentImageUrlList.stream().limit(1).collect(Collectors.toList());
                wmNews.setType((short) 1);//单图布局
            } else {
                //自动匹配规则3：如果内容图片地址数量大于等于0，布局决定为无图布局
                coverImageUlrList = new ArrayList<>();
                wmNews.setType((short) 0);//无图布局
            }

            //2.2 如果匹配到了封面图片，则将封面图片地址转为逗号拼接的字符串
            if (coverImageUlrList.size() > 0) {
                String coverImageStr = coverImageUlrList.stream().collect(Collectors.joining(","));
                wmNews.setImages(coverImageStr);
            }

            //2.3 更新文章的布局方式和封面图片
            this.updateById(wmNews);
        }

        //3 保存封面图片与文章的关系到关系表
        if (coverImageUlrList != null && coverImageUlrList.size() > 0) {
            responseResult = this.saveRelation(coverImageUlrList, wmNews.getId(), (short) 1);
            if (responseResult != null) {
                return responseResult;
            }
        }
        return null;
    }

    /**
     * 从文章内容中抽取全部图片地址列表
     *
     * @param dto
     * @return
     */
    private static List<String> extractContentImageUrlList(WmNewsDto dto) {
        List<String> contentImageUrlList = new ArrayList<>();
        List<Map> mapList = JSON.parseArray(dto.getContent(), Map.class);
        if (mapList != null && mapList.size() > 0) {
            for (Map<String, String> map : mapList) {
                String type = map.get("type");
                if ("image".equals(type)) {
                    String contentImageUrl = map.get("value");//内容中的图片地址
                    contentImageUrlList.add(contentImageUrl);
                }
            }
        }
        return contentImageUrlList;
    }

    @Autowired
    private WmNewsMaterialService wmNewsMaterialService;

    @Autowired
    private WmMaterialService wmMaterialService;

    private ResponseResult saveOrUpdateWmNews(WmNewsDto dto, Short isSubmit, WmNews wmNews) {
        //补充设置更多属性的值
        wmNews.setUserId(WmThreadLocalUtil.getUser().getId());//自媒体用户id
        wmNews.setEnable((short) 1);//默认值：已上架
        wmNews.setStatus(isSubmit);//0-保存草稿 1-提交审核
        wmNews.setSubmitedTime(new Date());//提交时间

        //ID无值，就创建文章
        if (wmNews.getId() == null) {
            wmNews.setCreatedTime(new Date());//创建时间
            this.save(wmNews);
        } else {
            //ID有值，就修改文章且删除关系

            //判断文章是否存在
            Integer count = this.lambdaQuery().eq(WmNews::getId, dto.getId()).count();
            if (count == 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "文章不存在无法修改");
            }

            //修改文章
            this.updateById(wmNews);

            //删除关系 delete from wm_news_material where news_id=?
            wmNewsMaterialService.remove(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getNewsId, wmNews.getId()));
        }
        return null;
    }

    /**
     * 公共方法：保存素材与文章的对应关系到关系表中
     *
     * @param imageUrlList 素材列表
     * @param wmNewsId     自媒体文章id
     * @param type         引用类型0-内容引用 1-主图引用
     * @return
     */
    private ResponseResult saveRelation(List<String> imageUrlList, Integer wmNewsId, Short type) {
        //1 根据内容中的素材地址从素材表中查询素材列表 select id from wm_material where url in (?,?,?...)
        List<WmMaterial> wmMaterialList = wmMaterialService.lambdaQuery().in(WmMaterial::getUrl, imageUrlList)
                .select(WmMaterial::getId).list();

        //2 判断是否所有素材地址都查询到了素材
        if (wmMaterialList.size() != imageUrlList.size()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.MATERIASL_REFERENCE_FAIL);
        }

        //3 获取素材列表中的所有素材ID列表
        List<Integer> materialIdList = wmMaterialList.stream().map(WmMaterial::getId).collect(Collectors.toList());

        //4 遍历素材ID列表逐一构建关系数据
        Short ord = 0;
        List<WmNewsMaterial> wmNewsMaterialList = new ArrayList<>();
        for (Integer materialId : materialIdList) {
            WmNewsMaterial wmNewsMaterial = new WmNewsMaterial();
            wmNewsMaterial.setNewsId(wmNewsId); //文章id
            wmNewsMaterial.setMaterialId(materialId); //素材id
            wmNewsMaterial.setType(type);//引用类型：0-内容引用  1-主图引用
            wmNewsMaterial.setOrd(ord++);//序号

            wmNewsMaterialList.add(wmNewsMaterial);
        }

        //5 批量保存关系数据 insert into wm_news_material (news_id,material_id,type,ord) values (1,101,0,1),(1,102,0,2),(1,103,0,3)
        wmNewsMaterialService.saveBatch(wmNewsMaterialList);

        return null;
    }


    /**
     * 文章上下架
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult downOrUp(WmNewsDto dto) {
        //TODO
        return ResponseResult.okResult("文章上下架完成");
    }


    //获取百度审核token
//    private String getAccessToken() throws IOException, JSONException {
//        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
//        RequestBody body = RequestBody.create(mediaType,"grant_type=client_credentials&client_id=" + API_KEY
//                + "&client_secret=" + SECRET_KEY);
//        Request request = new Request.Builder()
//                .url("https://aip.baidubce.com/oauth/2.0/token")
//                .method("POST", body)
//                .addHeader("Content-Type", "application/x-www-form-urlencoded")
//                .build();
//        Response response = HTTP_CLIENT.newCall(request).execute();
//        return new JSONObject(response.body().string()).getString("access_token");
//    }
//
//    //自动审核
//    private RequestBody autoAuditContent(WmNewsDto dto) throws JSONException, IOException {
//        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
//        RequestBody body = RequestBody.create(mediaType, dto.getContent());
//        Request request = new Request.Builder()
//                .url("https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" + getAccessToken())
//                .method("POST", body)
//                .addHeader("Content-Type", "application/x-www-form-urlencoded")
//                .build();
//        Response response = HTTP_CLIENT.newCall(request).execute();
//    }

    /*
     *
     * 自动审核
     * */
    private ResponseResult autoAuditContent(WmNewsDto dto) throws org.json.JSONException {
        try {
            List<String> images = extractimagesList(dto);
            List<String> text = extracttextList(dto);
            // 调用百度自动审核接口
            String accessToken = getAccessToken();  // 获取access_token
            String apiUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" + accessToken;

            MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
            String body1 = "text=" + text;// 请求参数：文章内容
            String body2 = "image=" + images;// 请求参数：文章内容
            RequestBody requestBody1 = RequestBody.create(mediaType, body1);
            RequestBody requestBody2 = RequestBody.create(mediaType, body2);

            // 发送POST请求
            Request request1 = new Request.Builder()
                    .url(apiUrl)
                    .post(requestBody1)
                    .addHeader("Content-Type", "application/x-www-form-urlencoded")
                    .build();
            Request request2 = new Request.Builder()
                    .url(apiUrl)
                    .post(requestBody2)
                    .addHeader("Content-Type", "application/x-www-form-urlencoded")
                    .build();

            // 使用 try-with-resources 语句确保 Response 对象被正确关闭
            try (Response response1 = HTTP_CLIENT.newCall(request1).execute()) {
                // 解析响应
                JSONObject responseJson1 = new JSONObject(response1.body().string());
                String errorCode = responseJson1.getString("error_code");

                // 判断是否通过审核并更新文章状态
                LambdaUpdateWrapper<WmNews> updateWrapper = Wrappers.lambdaUpdate();
                updateWrapper.eq(WmNews::getId, dto.getId());  // 根据 ID 更新状态

                if ("0".equals(errorCode)) {  // 如果error_code为0，表示通过审核
                    updateWrapper.set(WmNews::getStatus, 8);// 审核通过，设置状态为 8
                    wmNewsMapper.update(null, updateWrapper);
                } else {
                    updateWrapper.set(WmNews::getStatus, 2);  // 审核未通过，设置状态为 2
                    wmNewsMapper.update(null, updateWrapper);
                }
            }
            try (Response response2 = HTTP_CLIENT.newCall(request2).execute()) {
                // 解析响应
                JSONObject responseJson2 = new JSONObject(response2.body().string());
                String errorCode = responseJson2.getString("error_code");

                // 判断是否通过审核并更新文章状态
                LambdaUpdateWrapper<WmNews> updateWrapper = Wrappers.lambdaUpdate();
                updateWrapper.eq(WmNews::getId, dto.getId());  // 根据 ID 更新状态

                if ("0".equals(errorCode)) {  // 如果error_code为0，表示通过审核
                    updateWrapper.set(WmNews::getStatus, 8);// 审核通过，设置状态为 8
                    wmNewsMapper.update(null, updateWrapper);
                } else {
                    updateWrapper.set(WmNews::getStatus, 2);  // 审核未通过，设置状态为 2
                    wmNewsMapper.update(null, updateWrapper);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "自动审核请求失败");
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }

        return ResponseResult.okResult("自动审核完成");
    }


    /**
     * 获取百度API的AccessToken
     *
     * @return AccessToken
     * @throws IOException IO异常
     */
    private String getAccessToken() throws IOException, JSONException, org.json.JSONException {
        String API_KEY = "SQAns6D0AKN4Hjjj2wHwPX9W";
        String SECRET_KEY = "VPVCbOmRPEF1BBJ29eC6mUXSYiaWrsoh";
        String apiUrl = "https://aip.baidubce.com/oauth/2.0/token";
        String body = "grant_type=client_credentials&client_id=" + API_KEY + "&client_secret=" + SECRET_KEY;

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody requestBody = RequestBody.create(mediaType, body);

        Request request = new Request.Builder()
                .url(apiUrl)
                .post(requestBody)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();

        // 使用 try-with-resources 语句确保 Response 对象被正确关闭
        try (Response response = HTTP_CLIENT.newCall(request).execute()) {
            JSONObject responseJson = new JSONObject(response.body().string());
            return responseJson.getString("access_token");
        }
    }

    private static List<String> extractimagesList(WmNewsDto dto) {
        List<String> imagesList = new ArrayList<>();
        List<Map> mapList = JSON.parseArray(dto.getContent(), Map.class);
        if (mapList != null && mapList.size() > 0) {
            for (Map<String, String> map : mapList) {
                String type = map.get("type");
                if ("image".equals(type)) {
                    String content1 = map.get("value");//内容中的图片地址
                    imagesList.add(content1);
                }
            }
        }
        return imagesList;
    }
    private static List<String> extracttextList(WmNewsDto dto) {
        List<String> txtList = new ArrayList<>();
        List<String> imagesList = new ArrayList<>();
        List<Map> mapList = JSON.parseArray(dto.getContent(), Map.class);
        if (mapList != null && mapList.size() > 0) {
            for (Map<String, String> map : mapList) {
                String type = map.get("type");
                if ("text".equals(type)) {
                    String content2 = map.get("value");//内容中的图片地址
                    txtList.add(content2);
                }
            }
        }
        return txtList;
    }
}
